//================核心商品库.js


//修改领域模型单个属性
function updateBiField(formListId, serviceName, msg) {

    var selectKeys = h3ui.widgets[formListId].props.selectedRowKeys;
    let length = selectKeys.length;
    if (length == 0) {
        h3ui.$info({title: "请选择数据"});
        return false;
    }

    h3ui.$confirm({
        title: msg + '确认提交吗',
        onOk() {
            h3ui.services[serviceName]({ids: selectKeys}).then(res => {
                if (res.success) {
                    h3ui.$message.success('提交成功');

                    reloadList(formListId);
                } else {
                    h3ui.$message.error(res.errorMessage || '提交失败');
                }
            })
        }
    });
}


//触发列表刷新事件
function reloadList(fromListId) {
    doReloadList({pageIndex: 1, node: {id: fromListId}});
}


//列表刷新
function doReloadList(evt) {
    var list = h3ui.widgets[evt.node.id];

    var queryId = list.childIds.find(a => a.includes("ModelQueryForm"));

    var filter = h3ui.widgets[queryId].props.value || {};

    var pageId = list.childIds.find(a => a.includes("Pagination"));

    var pageIndex = h3ui.widgets[pageId].props.current;
    var pageSize = h3ui.widgets[pageId].props.pageSize;


    list.emit("load", {pageIndex, pageSize, filter});


    list.props.selectedRowKeys = [];
    list.props.selectedRow = [];

}


//核心品牌修改
async function updateCore(evt) {
    evt.node.props.disabled = true;
    var formListId = "FormList10";

    if (h3ui.widgets[formListId].props.selectedRow.find(a => a.core == 0)) {
        h3ui.$info({title: "所选产品已包含非核心品牌"});
    } else {
        await updateBiField(formListId, "updateGoodsesCore", "核心品牌修改");
    }

    evt.node.props.disabled = false;
}


function actionBar(evt) {
    console.log(evt);
    var listId = evt.node.id;
    if (evt.data.code == 'custExport') {
        opentExportSelect(listId);
    }

}


// 打开 导出选项弹框
function opentExportSelect(listId) {
    var keys = h3ui.widgets[listId].props.selectedRowKeys;
    if (keys.length == 0) {
        h3ui.$message.info("请选择至少一条数据");
        return;
    }

    h3ui.widgets.RadioGroup3.props.listId = listId;

    var modalProps = h3ui.widgets.Modal1.props;
    modalProps.visible = true;
    modalProps.confirmLoading = false;
}

//  导出
function postExport() {
    var modalProps = h3ui.widgets.Modal1.props;
    var listId = h3ui.widgets.RadioGroup3.props.listId;
    var ids = h3ui.widgets[listId].props.selectedRowKeys;
    if (ids.length == 0) {
        h3ui.$message.info("请选择至少一条数据");
        modalProps.visible = false;
        return;
    }
    var fileType = h3ui.widgets.RadioGroup3.props.value;
    if (!fileType) {
        h3ui.$message.info("请选择导出类型");
        return;
    }
    modalProps.confirmLoading = true;
    h3ui.services.exportGoodsPurchaseReport({data: {ids, fileType}}).then(res => {
        if (res.success) {
            //调用下载全局弹框

            modalProps.visible = false;
            h3ui.widgets[listId].props.selectedRowKeys = [];
            h3ui.widgets[listId].props.selectedRow = [];
            //打开下载弹框列表
            h3ui.widgets.gmNoticeBar1.props.visible = true;

        } else {
            h3ui.$message.error(res.errMessage || "导出失败");
        }
        modalProps.confirmLoading = false;
    })
}